/*******************************************************************************
 * Copyright (c) 2010 Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/
package ids.dev.managedbeans;

import ids.dev.model.SysUser;
import ids.dev.persistence.SysUserDAO;
import ids.dev.util.IDSPagesUtil;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;


/**
 * Created by JBoss Tools
 */
@Named
@ViewScoped
public class SecurityMB implements Serializable{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	

	@Inject
	SysUserDAO sysUserBO;
	
	private boolean firstAccess;
	private SysUser loggedUser;
	
	@PostConstruct
	public void init(){
		System.out.println("[LOG - INIT_MB] SecurityMB #################");
		this.loggedUser = new SysUser();
	}
	
	public void displayNewAccountFields(){
		this.firstAccess = Boolean.TRUE;
	}
	
	public void hideNewAccountFields(){
		this.firstAccess = Boolean.FALSE;
	}
	
	public String login() {
		//if(isFirstAccess()){
//		if(StringUtils.isNotEmpty(this.loggedUser.getFullName())){
//			System.out.println("[IDS - DEBUG] Creating a New User...");
//			sysUserBO.save(this.loggedUser);
//			System.out.println("[IDS - DEBUG] New User Created!");
//			return IDSPagesUtil.URL_DASHBOARD;
//			
//		}else{
//			System.out.println("[IDS - DEBUG] Logging User...");
//			//TODO Create a new way to authenticate our new friends.
//			SysUser loggedSysUser  = sysUserBO.find(Integer.parseInt(this.loggedUser.getUsername()));
//			if(loggedSysUser != null){
//				System.out.println("[IDS - DEBUG] User Logged! Calling to the Hooters!");
//				this.loggedUser = loggedSysUser;
//				return IDSPagesUtil.URL_DASHBOARD;	
//				
//			}else{
//				return IDSPagesUtil.URL_WELCOME;
//				
//			}
//		}
		return IDSPagesUtil.URL_INVOICE_VIEW;
	}

	public boolean isFirstAccess() {
		return firstAccess;
	}

	public void setFirstAccess(boolean firstAccess) {
		this.firstAccess = firstAccess;
	}

	public SysUser getLoggedUser() {
		return loggedUser;
	}

	public void setLoggedUser(SysUser loggedUser) {
		this.loggedUser = loggedUser;
	}
	
	
	
}